module.exports = function (success, error){
    // 设置默认参数
    if (typeof error !== 'function') {
        error = function () {
            console.log('数据库连接失败');
        }
    }

    // 引入mongoose
    const mongoose = require('mongoose');
    const { port, host, db } = require('./config.js');
    
    // 设置连接参数
    mongoose.set('strictQuery', true);
    
    // 连接数据库
    mongoose.connect(`mongodb://${host}:${port}/${db}`);

    // 监听连接成功
    mongoose.connection.once('open', () => {
        success();
    })  
    
    // 监听连接失败
    mongoose.connection.once('error', () => {
        error();
    })
    
    // 监听连接关闭
    mongoose.connection.once('close', () => {
        console.log('数据库连接关闭');
    })
}